package com.tanhua.system.controller;

import com.tanhua.system.service.DashboardService;
import com.tanhua.system.service.DashboardServicePlus;
import com.tanhua.system.vo.DashBoardVo;
import com.tanhua.system.vo.DashBoardYear;
import com.tanhua.system.vo.DashboardDistribution;
import com.tanhua.system.vo.DashboardResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: Mr.blue
 * @Description:
 * @Date: Created in 10:51 2021/1/17
 * @ModifiedBy:
 */
@RestController
@RequestMapping("dashboard")
@Slf4j
public class DashboardController {


    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private DashboardServicePlus dashBoardServicePlus;

    /**
     * 概要统计信息
     * @return 状态信息
     */
    @GetMapping("summary")
    public ResponseEntity<DashboardResult> querySummary() {
        try {
            DashboardResult dashboardResult = dashboardService.queryDashboard();
            if(dashboardResult != null){
                return ResponseEntity.ok(dashboardResult);
            }
        } catch (Exception e) {
            log.error("查询数据出错~", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

    /**
     * 新增,活跃用户,次日留存率
     * @param sd 开始时间
     * @param ed 结束时间
     * @param type 101 新增 102 活跃用户 103 次日留存率
     * @return 状态信息
     */
    @GetMapping("users")
    public ResponseEntity<DashBoardVo> dashBoardMsg(@RequestParam("sd") Long sd,
                                                    @RequestParam("ed") Long ed,
                                                    @RequestParam("type") Integer type){
        try {
            DashBoardVo dashBoardVo = this.dashBoardServicePlus.dashBoardMsg(sd,ed,type);
            if(dashBoardVo!=null){
                return ResponseEntity.ok(dashBoardVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }




    @GetMapping("distribution")
    public ResponseEntity<DashboardDistribution> queryUsers(@RequestParam(value = "sd")String sd,
                                                            @RequestParam(value = "ed")String ed) {
        try {
            DashboardDistribution dashboardDistribution = dashboardService.queryDashboardOtherData(sd,ed);
            if(dashboardDistribution != null){
                return ResponseEntity.ok(dashboardDistribution);
            }
        } catch (Exception e) {
            log.error("查询数据出错~", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }
}
